/*
 * Copyright (c) 2012-2013, chzh.xiao technology Co., Ltd.
 * 
 * All rights reserved.
 */
package com.yuanxin.app.appjob.controller.pc;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.yuanxin.app.appjob.appobject.PersonInfoAO;
import com.yuanxin.app.appjob.appobject.UserExtAO;
import com.yuanxin.app.appjob.common.MVCViewName;
import com.yuanxin.app.appjob.entity.gen.PersonInfoCriteria;
import com.yuanxin.app.appjob.entity.gen.UserExtCriteria;
import com.yuanxin.app.appjob.service.IUserExtService;
import com.yuanxin.app.appjob.service.IPersonInfo;
import com.yuanxin.app.appjob.util.Constant;
import com.yuanxin.app.appjob.util.CheckLoginInterCeptor;
import com.yuanxin.framework.logging.Logger;
import com.yuanxin.framework.logging.LoggerFactory;
import com.yuanxin.framework.service.ServiceResult;
import com.yuanxin.service.biz.userbasesrv.appobject.UserBaseAO;
import com.yuanxin.service.biz.userbasesrv.service.login.ILoginService;
/**
 * Hello控制器。
 * 
 */
@Controller
@RequestMapping(value = "/pc/login")
public class LoginController {
	
	@Resource
	IPersonInfo userService;
	
	@Resource
	IUserExtService userExtService;
	
	@Resource
	ILoginService loginService;

    private static Logger LOG = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping(value = "", method = { RequestMethod.GET, RequestMethod.POST })
    public String hello(Model model,HttpServletRequest request, HttpServletResponse response, HttpSession session){
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setContentType("text/html;charset=utf-8"); 
    	
        LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
        LOG.info("SessionId %s", request.getSession().getId());
        //将当前运用名称传到前端
        return MVCViewName.APP_APPJOB_PC_BASIC_LOGIN.toString();
    }
    
    @RequestMapping(value = "dologin")
    @ResponseBody
	public Object login(final Model model, HttpSession session, HttpServletRequest request) {
		//验证
		String xLoginCode = request.getParameter("xLoginCode");
		String password = request.getParameter("password");
		ServiceResult<String> result = new ServiceResult<String>();
		ServiceResult<Boolean> ret = loginService.doLoginByLoginName(xLoginCode, password,
				request.getSession().getId());
		if (ret.isSucceed()) {
    	//if (true) {
			PersonInfoCriteria uc = new PersonInfoCriteria();
			uc.createCriteria().andStaffAccountEqualTo(xLoginCode);
			ServiceResult<List<PersonInfoAO>> uList = userService.selectByCriteria(uc);
			PersonInfoAO u = uList.getData().get(0);
			WebUtils.setSessionAttribute(request, Constant.SESSION_CURRENT_USER, u);
			//UserBaseAO u = (UserBaseAO) ret.getAdditionalProperties("userbase");
			//WebUtils.setSessionAttribute(request, Constant.SESSION_CURRENT_USER, u);
			//保存改用户扩展信息
			UserExtCriteria uec = new UserExtCriteria();
			uec.createCriteria().andUserCodeEqualTo(u.getStaffNum());
			ServiceResult<List<UserExtAO>> temp = userExtService.selectByCriteria(uec);
			if (temp != null && temp.getData() != null && temp.getData().size() > 0) {
				UserExtAO ueao = temp.getData().get(0);
				ueao.setPsd(password);
				ueao.setxLoginName(xLoginCode);
				userExtService.saveOrUpdate(ueao);
			}else{
				//查不到改用户扩展信息，则创建。
				UserExtAO ueao = new UserExtAO();
				ueao.setUserCode(u.getStaffNum());
				ueao.setPsd(password);
				ueao.setxLoginName(xLoginCode);
				userExtService.saveOrUpdate(ueao);
			}
			result.setSucceed(true);
			result.setData("homepage");
		} else {
			result.setData("账号或密码错误！");
			result.setSucceed(false);
		}
		return result;
	}
    
    @RequestMapping(value = "logout", method = { RequestMethod.GET, RequestMethod.POST })
    public String logout(Model model,HttpServletRequest request, HttpServletResponse response, HttpSession session){
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setContentType("text/html;charset=utf-8"); 
    	
    	LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
    	LOG.info("SessionId %s", request.getSession().getId());
    	//将当前运用名称传到前端
    	request.getSession().removeAttribute(Constant.SESSION_CURRENT_USER);
        
    	return MVCViewName.APP_APPJOB_PC_BASIC_LOGIN.toString();
    }
    @RequestMapping(value = "/homepage", method = { RequestMethod.GET, RequestMethod.POST })
    public String homepage(Model model,HttpServletRequest request, HttpServletResponse response, HttpSession session){
    	LOG.info("有访问来自，IP: %s USER-AGENT: %s", request.getRemoteAddr(), request.getHeader("user-agent"));
    	LOG.info("SessionId %s", request.getSession().getId());
    	return MVCViewName.APP_APPJOB_PC_BASIC_HOMEPAGE.toString();
    }
}
